<?php


/*
 * PosterousClient.php
 *
 * Created by Dmitry Shirokov @ 2010-12-12.
 * Copyright 2010 deadrunk@gmail.com. All rights reserved.
 *
 * $Id: PosterousClient.php 2 2010-12-25 21:21:37Z deadrunk@gmail.com $
 */

class PosterousClient
{
	
	private $_pass;
	private $_user;
	private $_authToken;
	public $debug = false;
	
	const API_URL       = 'http://posterous.com/api/2';
	const METHOD_GET    = 'GET';
	const METHOD_POST   = 'POST';
	const METHOD_DELETE = 'DELETE';
	const METHOD_PUT 	= 'PUT';
	
	public function __construct($user, $pass)
	{
		$this->_user = $user;
		$this->_pass = $pass;
	}
	
	public function auth()
	{
		$token = $this->get('/auth/token');
		
		if (empty($token->api_token)) {
			throw new Exception('There is a problem with authentication');
		}
		
		$this->_authToken = $token->api_token;
	}
	
	public function get($method)
	{
		return $this->_call(self::METHOD_GET, $method);
	}
	
	public function post($method, $params = null)
	{
		return $this->_call(self::METHOD_POST, $method, $params);
	}
	
	public function delete($method)
	{
		return $this->_call(self::METHOD_DELETE, $method);
	}
	
	public function put($method)
	{
		return $this->_call(self::METHOD_PUT, $method);
	}
	
	private function _call($type, $method, $params = array())
	{
		$this->_debug($type . ' -> ' . $method);
		
		if (empty($this->_authToken) && $method != '/auth/token') {
			$this->auth();
		}
		
		$methodUrl = self::API_URL . $method;
		$ch = curl_init();
		
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
		
		if ($type == self::METHOD_GET) {
			if (!empty($params)) {
				foreach ($params as $key => $value) {
					$methodUrl .= sprintf('/%s/%s', $key, $value);
				}
			}
			$methodUrl .= '?api_token=' . $this->_authToken;
			
		} elseif ($type == self::METHOD_POST) {
			$params['api_token'] = $this->_authToken;
			if (!empty($params)) {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
			}
			
		} elseif ($type == self::METHOD_DELETE) {
			$methodUrl .= '?api_token=' . $this->_authToken;
			
		} elseif ($type == self::METHOD_PUT) {
			$params['api_token'] = $this->_authToken;
			if (!empty($params)) {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
			}
			
		} else {
			throw new Exception('Unknown request method, sorry');
		}
		
		curl_setopt($ch, CURLOPT_URL, $methodUrl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $this->_user, $this->_pass));
				
		$data = curl_exec($ch);
		$this->_debug($data);
		$data = json_decode($data);
		curl_close($ch);
		
		if (isset($data->error)) {
			throw new Exception('Posterous Api Client error: ' . $data->error);
		}
		
		return $data;
		
	}
	
	private function _debug($thing)
	{
		if ($this->debug == false) return;
		
		if (is_string($thing)) {
			printf("%s\n", $thing);
		} else {
			printf("%s\n", json_encode($thing));
		}
	}
	
}
?>